﻿
/*
NAILSUPERSTORE ® COMPANY
 
[HISTORY]
Created By  	: NGUYEN VAN TY
Created Date	: 12/14/2010
Modified By     :
Modified Date   :
Note            :

*/

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using CommonTagTime;
using CommonTagTime.ModelObject;
using CommonTagTime.Business;
namespace CommonTagTime.DataAccess
{
    public class daoDistributor
    {
        public daoDistributor()
        {

        }
        public DistributorCollection List(Distributor distributor)
        {

            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[6];
            SqlDataReader de;
            DistributorCollection items = new DistributorCollection();
            try
            {
                para[0] = new SqlParameter(busCategory.ORDERBY, SqlDbType.NVarChar);
                para[0].Value = distributor.OrderBy;
                para[1] = new SqlParameter(busCategory.ORDERDIRECTION, SqlDbType.NVarChar);
                para[1].Value = distributor.OrderDirection;
                para[2] = new SqlParameter(busCategory.CURRENTPAGE, SqlDbType.Int);
                para[2].Value = distributor.CurrentPage;
                para[3] = new SqlParameter(busCategory.PAGESIZE, SqlDbType.Int);
                para[3].Value = distributor.PageSize;
                para[4] = new SqlParameter(busCategory.CONDITION, SqlDbType.NVarChar);
                para[4].Value = distributor.Condition;
                para[5] = new SqlParameter(busCategory.TOTALRECORDS, SqlDbType.Int);
                para[5].Direction = ParameterDirection.Output;
                de = clsSQL.SPDataReader("Distributor_List", para);
                while (de.Read())
                {
                    Distributor item = Load(de);
                    items.Add(item);
                }

                de.Close();
                distributor.TotalRecords = Convert.ToInt32(para[5].Value);
                de.Dispose();
            }
            catch (Exception e)
            {
                clsFile.WriteLog("Distributor", "List(Distributor distributor)", e.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }
            return items;
        }     


        private Distributor Load(IDataReader de)
        {
            Distributor item = new Distributor();
            item.DistributorID = clsConvert.DbToInt("DistributorID", de);
            item.Address1 = clsConvert.DbToString("Address1", de);
            item.Address2 = clsConvert.DbToString("Address2", de);
            item.City = clsConvert.DbToString("City", de);
            item.CompanyName = clsConvert.DbToString("CompanyName", de);
            item.ContactName = clsConvert.DbToString("ContactName", de);
            item.CountryCode = clsConvert.DbToString("CountryCode", de);
            item.Email = clsConvert.DbToString("Email", de);
            item.Fax = clsConvert.DbToString("Fax", de);
            item.IsActive = clsConvert.DbToBoolean("IsActive", de);
            item.Note = clsConvert.DbToString("Note", de);
            item.Phone = clsConvert.DbToString("Phone", de);
            item.Province = clsConvert.DbToString("Province", de);
            item.StateCode = clsConvert.DbToString("StateCode", de);
            item.ZipCode = clsConvert.DbToString("ZipCode", de);            
            return item;
            // de.P
            //int intLogID = Convert.ToInt32(_comm.Parameters["@LogID"].Value);
        }
        public DistributorCollection GetByCountryId(int countryId)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            SqlDataReader de;
            DistributorCollection items = new DistributorCollection();
            try
            {
                para[0] = new SqlParameter(busCountry.COUNTRYID, SqlDbType.Int);
                para[0].Value = countryId;
                de = clsSQL.SPDataReader("Distributor_GetByCountryID", para);
                while (de.Read())
                {
                    Distributor item = Load(de);
                    items.Add(item);
                }
                de.Close();                
                de.Dispose();
            }
            catch (Exception e)
            {
                clsFile.WriteLog("daoDistributor", "GetByCountryId(int countryId)", e.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }
            return items;
        }     

        public bool Insert(Distributor objDistributor)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[14];
            try
            {
                para[0] = new SqlParameter(busDistributor.COUNTRYCODE, SqlDbType.NVarChar);
                para[0].Value = clsConvert.StringToDB(objDistributor.CountryCode);
                para[1] = new SqlParameter(busDistributor.COMPANYNAME, SqlDbType.NVarChar);
                para[1].Value = clsConvert.StringToDB(objDistributor.CompanyName);
                para[2] = new SqlParameter(busDistributor.CONTACTNAME, SqlDbType.NVarChar);
                para[2].Value = clsConvert.StringToDB(objDistributor.ContactName);
                para[3] = new SqlParameter(busDistributor.ADDRESS1, SqlDbType.NVarChar);
                para[3].Value = clsConvert.StringToDB(objDistributor.Address1);
                para[4] = new SqlParameter(busDistributor.ADDRESS2, SqlDbType.NVarChar);
                para[4].Value = clsConvert.StringToDB(objDistributor.Address2);
                para[5] = new SqlParameter(busDistributor.STATECODE, SqlDbType.NVarChar);
                para[5].Value = clsConvert.StringToDB(objDistributor.StateCode);
                para[6] = new SqlParameter(busDistributor.ZIPCODE, SqlDbType.NVarChar);
                para[6].Value = clsConvert.StringToDB(objDistributor.ZipCode);
                para[7] = new SqlParameter(busDistributor.CITY, SqlDbType.NVarChar);
                para[7].Value = clsConvert.StringToDB(objDistributor.City);
                para[8] = new SqlParameter(busDistributor.PROVINCE, SqlDbType.NVarChar);
                para[8].Value = clsConvert.StringToDB(objDistributor.Province);
                para[9] = new SqlParameter(busDistributor.PHONE, SqlDbType.NVarChar);
                para[9].Value = clsConvert.StringToDB(objDistributor.Phone);
                para[10] = new SqlParameter(busDistributor.FAX, SqlDbType.NVarChar);
                para[10].Value = clsConvert.StringToDB(objDistributor.Fax);
                para[11] = new SqlParameter(busDistributor.EMAIL, SqlDbType.NVarChar);
                para[11].Value = clsConvert.StringToDB(objDistributor.Email);
                para[12] = new SqlParameter(busDistributor.NOTE, SqlDbType.NVarChar);
                para[12].Value = clsConvert.StringToDB(objDistributor.Note);
                para[13] = new SqlParameter(busCategory.ISACTIVE, SqlDbType.Bit);
                para[13].Value = objDistributor.IsActive;              
                result = clsSQL.SPExecute("Distributor_Insert", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoDistributor", "Distributor_Insert(Distributor objDistributor)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }


        public bool DeActive(int DistributorID)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busDistributor.DISTRIBUTORID, SqlDbType.Int);
                para[0].Value =DistributorID;
                result = clsSQL.SPExecute("Distributor_DeActive", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoDistributor", "DeActive(DistributorID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public bool Active(int DistributorID)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busDistributor.DISTRIBUTORID, SqlDbType.Int);
                para[0].Value = DistributorID;
                result = clsSQL.SPExecute("Distributor_Active", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoDistributor", "Active(DistributorID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }

        public bool Delete(int DistributorID)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busDistributor.DISTRIBUTORID, SqlDbType.Int);
                para[0].Value = DistributorID;
                result = clsSQL.SPExecute("Distributor_Delete", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoDistributor", "Delete(DistributorID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public Distributor GetByID(int _distributorID)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            SqlDataReader de;
            Distributor item = new Distributor();

            try
            {
                para[0] = new SqlParameter(busDistributor.DISTRIBUTORID, SqlDbType.Int);
                para[0].Value = _distributorID;
                using (de = clsSQL.SPDataReader("Distributor_GetByID", para))
                {
                    while (de.Read())
                    {
                        item = Load(de);
                    }

                    de.Close();
                    de.Dispose();
                }
            }
            catch (Exception ex)
            {
                item = null;
                clsFile.WriteLog("daoDistributor", "GetByID(_distributorID)", ex.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }

            return item;
        }
        public bool Update(Distributor objDistributor)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[15];
            try
            {
                para[0] = new SqlParameter(busDistributor.COUNTRYCODE, SqlDbType.NVarChar);
                para[0].Value = clsConvert.StringToDB(objDistributor.CountryCode);
                para[1] = new SqlParameter(busDistributor.COMPANYNAME, SqlDbType.NVarChar);
                para[1].Value = clsConvert.StringToDB(objDistributor.CompanyName);
                para[2] = new SqlParameter(busDistributor.CONTACTNAME, SqlDbType.NVarChar);
                para[2].Value = clsConvert.StringToDB(objDistributor.ContactName);
                para[3] = new SqlParameter(busDistributor.ADDRESS1, SqlDbType.NVarChar);
                para[3].Value = clsConvert.StringToDB(objDistributor.Address1);
                para[4] = new SqlParameter(busDistributor.ADDRESS2, SqlDbType.NVarChar);
                para[4].Value = clsConvert.StringToDB(objDistributor.Address2);
                para[5] = new SqlParameter(busDistributor.STATECODE, SqlDbType.NVarChar);
                para[5].Value = clsConvert.StringToDB(objDistributor.StateCode);
                para[6] = new SqlParameter(busDistributor.ZIPCODE, SqlDbType.NVarChar);
                para[6].Value = clsConvert.StringToDB(objDistributor.ZipCode);
                para[7] = new SqlParameter(busDistributor.CITY, SqlDbType.NVarChar);
                para[7].Value = clsConvert.StringToDB(objDistributor.City);
                para[8] = new SqlParameter(busDistributor.PROVINCE, SqlDbType.NVarChar);
                para[8].Value = clsConvert.StringToDB(objDistributor.Province);
                para[9] = new SqlParameter(busDistributor.PHONE, SqlDbType.NVarChar);
                para[9].Value = clsConvert.StringToDB(objDistributor.Phone);
                para[10] = new SqlParameter(busDistributor.FAX, SqlDbType.NVarChar);
                para[10].Value = clsConvert.StringToDB(objDistributor.Fax);
                para[11] = new SqlParameter(busDistributor.EMAIL, SqlDbType.NVarChar);
                para[11].Value = clsConvert.StringToDB(objDistributor.Email);
                para[12] = new SqlParameter(busDistributor.NOTE, SqlDbType.NVarChar);
                para[12].Value = clsConvert.StringToDB(objDistributor.Note);
                para[13] = new SqlParameter(busDistributor.ISACTIVE, SqlDbType.Bit);
                para[13].Value = objDistributor.IsActive;
                para[14] = new SqlParameter(busDistributor.DISTRIBUTORID, SqlDbType.Int);
                para[14].Value = objDistributor.DistributorID;
                result = clsSQL.SPExecute("Distributor_Update", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoDistributor", "Update(Distributor distributor)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }



    }
}

